<template>
  <div id="background_image">
    <el-container>
      <el-header>系统登录</el-header>
      <div id="main">
        <el-row>
          <!--登录框居中-->
          <el-col :span="6" :offset="9">
            <div id="input">
              <el-row>
                <el-col>
                  <el-input
                    placeholder="请输入用户名"
                    v-model="username"
                    :maxlength="20"
                    clearable
                    prefix-icon="el-icon-s-custom"
                  ></el-input>
                </el-col>
              </el-row>
              <br />
              <el-row>
                <el-col>
                  <el-input
                    placeholder="请输入密码"
                    v-model="password"
                    show-password
                    prefix-icon="el-icon-lock"
                  ></el-input>
                </el-col>
              </el-row>
              <br />
              <el-button type="primary" @click="LoginFunc(username, password)"
                >登录</el-button
              >
            </div>
          </el-col>
        </el-row>
      </div>
    </el-container>
  </div>
</template>

<script>
import { ReqLogin, ReqAutoLogin } from "../../api/index";
export default {
  name: "login",
  data() {
    return {
      username: "",
      password: "",
    };
  },
  created() {
    //实现自动登录功能的接口，在本地存在用户信息，后台验证用户信息成功的情况下，实现自动登录的功能
    // 测试自动登录
    ReqAutoLogin().then((res) => {
      if (res.code === 1) {
        this.$message({
          message: "请先登录",
          type: "warning",
        });
      } else {
        this.$message({
          message: "自动登录成功",
          type: "success",
        });
        // 跳转到主页
        this.$router.replace("/admin");
      }
    });
  },
  methods: {
    LoginFunc(username, password) {
      if ((username, password)) {
        ReqLogin(username, password).then((res) => {
          if (res.code === 1) {
            this.$message({
              message: res.msg,
              type: "danger",
            });
          } else {
            this.$message({
              message: "登录成功",
              type: "success",
            })
            // 跳转到主页
            this.$router.replace("/admin");
          }
        });
      } else {
        this.$message({
          message: "请输入完整的用户名和密码",
          type: "warning",
        });
      }
    },
  },
};
</script>

<style scoped>
/*加入背景样式*/
@import "../../assets/static/css/background_image.css";
.el-header {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

#input {
  height: 200px;
  margin-top: 300px;
}
</style>
